<template>
  <div id="userLoginPage">
    <a-form
      :model="form"
      style="max-width: 480px; margin: 0 auto"
      @submit="handleSubmit"
      auto-label-width
      lebal-align="left"
    >
      <a-form-item field="name" label="用户名">
        <a-input
          v-model="form.userAccount"
          placeholder="请输入您的用户名"
          :style="{ width: '320px' }"
        />
      </a-form-item>
      <a-form-item field="post" label="密码">
        <a-input-password
          v-model="form.userPassword"
          placeholder="请输入您的密码"
          :style="{ width: '320px' }"
          :defaultVisibility="true"
          allow-clear
        />
      </a-form-item>
      <a-form-item>
        <a-space>
          <a-button html-type="submit" type="primary">登录</a-button>
          <a-button html-type="submit" type="primary" href="/user/register"
            >注册
          </a-button>
        </a-space>
        <a-link
          html-type="submit"
          type="primary"
          href="#"
          style="margin-left: 100px"
          >忘记密码
        </a-link>
      </a-form-item>
    </a-form>
    {{ form }}
  </div>
</template>

<script setup lang="ts">
import { reactive } from "vue";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";
import { useLoginUserStore } from "@/store/userStore";
import { userLoginUsingPost } from "@/api/userController";
import API from "@/api";

/**
 * 表单信息
 */
const form = reactive({
  userAccount: "",
  userPassword: "",
} as API.UserLoginRequest);

const router = useRouter();
const loginUserStore = useLoginUserStore();

/**
 * 提交表单
 * @param data
 */
const handleSubmit = async () => {
  const res = await userLoginUsingPost(form);
  // 登录成功，跳转到主页
  if (res.data.code === 0) {
    loginUserStore.setLoginUser(res.data.data as any);
    router.push({
      path: "/",
      replace: true,
    });
  } else {
    message.error("登录失败，" + res.data.message);
  }
};
</script>
